<?php
/**
 * User: hjd-yao
 * Date: 2019/2/26
 * Time: 14:46
 */

namespace App\Http\Controllers\Cms;


use App\Helpers\SelfResponse;
use App\Http\Controllers\BackendController;
use App\Http\Requests\Cms\MessageSendRequest;
use App\Models\Cms\MessageModel;
use Carbon\Carbon;
use DC\UserCenter\Models\UserBaseModel;
use Illuminate\Http\Request;

class MessageController extends BackendController
{
    private static $_messageModel;

    public function __construct()
    {
        self::$_messageModel = new MessageModel();
    }

    /**
     * @title 留言列表
     * @param Request $request
     * @param string $type
     * @return View
     */
    public function index(Request $request,$type = 'inbox')
    {
        self::$_messageModel = self::$_messageModel->with(['fromUser'=>function($query){
            $query->select(['member_id','user_number','real_name']);
        },'toUser'=>function($query){
            $query->select(['member_id','user_number','real_name']);
        }]);
        if($type == 'inbox'){
            self::$_messageModel = self::$_messageModel->where('to_uid',0)
                ->orderBy('is_reply');
        }else{
            self::$_messageModel = self::$_messageModel->where('from_uid',0)
                ->orderBy('is_reply','desc');
        }
        $list = self::$_messageModel->paginate($this->getPerPageNum());

        return view('backend::cms.message.list',[
            'page_title'=>'消息列表',
            'type'=>$type,
            'list'=>$list
        ]);
    }

    /**
     * @title 留言详情
     * @param $id
     * @return View
     */
    public function detail($id)
    {
        $message = MessageModel::find($id);
        return view('backend::cms.message.detail',[
            'page_title'=>'留言信息',
            'message'=>$message
        ]);
    }

    /**
     * @title 发送留言
     * @param MessageSendRequest $request
     * @return SelfResponse|View
     */
    public function send(MessageSendRequest $request)
    {
        if($request->isMethod('post')){
            $to_user = $request->post('to_user');
            $to_user = format_message_user($to_user);
            $to_uids = UserBaseModel::whereIn('user_number',$to_user)->pluck('member_id');
            if ($to_uids->isEmpty()){
                return SelfResponse::error('留言的会员信息有误!');
            }
            $content = $request->post('content');
            $title = $request->post('title');
            $msg_img = $request->post('msg_img');
            foreach ($to_uids as $to_uid){
                $item['to_uid'] = $to_uid;
                $item['from_uid'] = 0;
                $item['title'] = $title;
                $item['content'] = $content;
                $item['msg_img'] = $msg_img;
                if(!MessageModel::create($item)){
                    continue;
                }
                unset($item);
            }
            return SelfResponse::success('留言成功！');
        }else{
            $user_id = $request->get('user_id',0);
            $user_info = false;
            if($user_id){
                $user_info = UserBaseModel::find($user_id,['user_number','real_name']);
            }
            return view('backend::cms.message.send',[
                'page_title'=>'留言',
                'user_info'=>$user_info
            ]);
        }
    }

    /**
     * @title 回复留言
     * @param Request $request
     * @param $id
     * @return SelfResponse|View
     */
    public function reply(Request $request,$id)
    {
        if($request->isMethod('post')){
            $reply = $request->post('reply');
            $reply_img = $request->post('reply_img');
            $info = MessageModel::find($id);
            $info->is_reply = 1;
            $info->is_read = 1;
            $info->reply = $reply;
            $info->reply_img = $reply_img;
            $info->replied_at = Carbon::now()->getTimestamp();
            if(!$info->save()){
                return SelfResponse::error('留言回复失败!');
            }
            return SelfResponse::success('留言回复成功');
        }else{
            return view('backend::cms.message.reply',[
                'page_title'=>'回复留言',
                'message'=>MessageModel::find($id)
            ]);
        }
    }

    /**
     * @title 标记已读
     * @param $id
     * @return SelfResponse
     */
    public function markedRead($id)
    {
        $info = MessageModel::find($id);
        if($info->from_uid>0&&$info->to_uid!=0){
            return SelfResponse::error('该留言与你无关,无须标记');
        }
        if ($info->from_uid==0&&$info->is_reply==1){ //系统留言
            $info->is_read = 2;
            if($info->save()){
                return SelfResponse::success('标记成功');
            }
        }
        if($info->to_uid==0&&$info->is_reply==0){ //给系统留言
            $info->is_read = 1;
            if($info->save()){
                return SelfResponse::success('标记成功');
            }
        }
        return SelfResponse::error('无须标记');
    }

    /**
     * @param $id
     */
    public function delete($id)
    {

    }

    /**
     * @title 获取未读信息
     * @return JsonResponse
     */
    public function ajaxGetMessage()
    {
        $list = self::$_messageModel->where('to_uid',0)
            ->where('is_reply',0)->where('is_read',0)->where('status',1)->select(['id','content','created_at'])
            ->orderBy('created_at')->limit(5)->get();
        return response()->json(['status'=>1,'data'=>$list]);
    }
}